package Week.w147;

import org.junit.Test;

import java.util.Arrays;

public class Solution01 {

    public int movesToMakeZigzag(int[] nums) {
        return  two(nums);
    }

    /**
     * 大小大
     *
     * @param nums
     * @return
     */
    private int one(int[] nums) {
        int result1=0;
        int result2=0;
        int[] nums1= Arrays.copyOf(nums,nums.length);
        for(int i=0;i<nums.length;i+=2){
            int stepI=getNumMaxStep(nums1,i);
            result1+=stepI;
        }
        int[] nums2= Arrays.copyOf(nums,nums.length);
        for(int i=1;i<nums.length;i+=2){
            int stepII=getNumMinStep(nums2,i);
            result2+=stepII;
        }
        return Math.min(result1,result2);
    }
    /**
     * 小大小
     *
     * @param nums
     * @return
     */
    private int two(int[] nums) {
        int result1=0;
        int result2=0;
        int[] nums1= Arrays.copyOf(nums,nums.length);
        for(int i=0;i<nums.length;i+=2){
            int stepI=getNumMinStep(nums1,i);
            result1+=stepI;
        }
        int[] nums2= Arrays.copyOf(nums,nums.length);
        for(int i=1;i<nums.length;i+=2){
            int stepII=getNumMaxStep(nums2,i);
            result2+=stepII;
        }
        return Math.min(result1,result2);
    }

    private int getNumMaxStep(int[] nums,int index) {
        if (nums.length <= 1) {
            return 0;
        }
        int num = nums[index];
        if (index == 0) {
            if (num < nums[index + 1]) {
                nums[index] = nums[index + 1] + 1;
                return nums[index + 1] - num + 1;
            }
        } else if (index + 1 >= nums.length) {
            if (num < nums[index - 1]) {
                nums[index] = nums[index - 1] + 1;
                return nums[index - 1] - num + 1;
            }
        } else {
            int max = Math.max(nums[index - 1], nums[index + 1]);
            if (num <= max) {
                nums[index] = max + 1;
                return max - num + 1;
            }
        }
        return 0;
    }

    private int getNumMinStep(int[] nums,int index) {
        if (nums.length <= 1) {
            return 0;
        }
        int num = nums[index];
        if (index == 0) {
            if (num > nums[index + 1]) {
                nums[index] = nums[index + 1] - 1;
                return num - nums[index + 1] + 1;
            }

        } else if (index + 1 >= nums.length) {
            if (num > nums[index - 1]) {
                nums[index] = nums[index - 1] - 1;
                return num - nums[index - 1] + 1;
            }
        } else {
            int min = Math.min(nums[index - 1], nums[index + 1]);
            if (num >=min) {
                nums[index] = min - 1;
                return num - min + 1;
            }
        }

        return 0;
    }



    @Test
    public void test() {
        int[] num = new int[]{1, 2, 3};

        int[] num2= new int[]{2,7,10,9,8,9};

        int[] num3=new int[]{7,4,8,9,7,7,5};

        int[] nums4=new int[]{3,10,7,9,9,3,6,9,4};
        System.out.println(movesToMakeZigzag(num3));
    }



}
